home *** CD-ROM | disk | FTP | other *** search
/ Greenhouse Effect Detection Expriment / NASA Greenhouse Effect Detection Expriment 1992 - Disc 2.iso / software / dos / cdf22pc / src / tools / rowtocol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  3.6 KB  |  110 lines

  1. /******************************************************************************
  2. *
  3. *  NSSDC/CDF                  Convert from ROW major to COLUMN major.
  4. *
  5. *  Version 1.2, 20-Feb-92, ST Systems (STX)
  6. *
  7. *  Modification history:
  8. *
  9. *   V1.0  15-May-91, J Love    Original version.
  10. *   V1.1  29-Oct-91, J Love    Added needed system include files.
  11. *   V1.2  20-Feb-92, J Love    Changed to have an input and output buffer
  12. *                rather than switching the majority in place.
  13. *
  14. ******************************************************************************/
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #define MAX_DIMS    10            /* same as CDF_MAX_DIMS */
  21.  
  22. #if defined(unix)
  23. #define memmove(a,b,x) bcopy(b,a,x)
  24. #endif
  25.  
  26. /******************************************************************************
  27. * Macro to calculate the indices for a given value offset (row major).
  28. ******************************************************************************/
  29.  
  30. #define ROWindices(valueOffset,Ndims,products,indices) { \
  31. long valueOffsetT = valueOffset; \
  32. long i; \
  33. for (i = 0; i < Ndims; i++) { \
  34.    indices[i] = valueOffsetT / products[i]; \
  35.    valueOffsetT = valueOffsetT % products[i]; \
  36. } \
  37. }
  38.  
  39. /******************************************************************************
  40. * Macro to calculate the value offset for a given set of indices (column
  41. * major).
  42. ******************************************************************************/
  43.  
  44. #define COLoffset(indices,Ndims,products,valueOffset) { \
  45. long i; \
  46. valueOffset = 0; \
  47. for (i = 0; i < Ndims; i++) valueOffset += indices[i] * products[i]; \
  48. }
  49.  
  50. /******************************************************************************
  51. * ROWtoCOL.
  52. ******************************************************************************/
  53.  
  54. void ROWtoCOL (iBuffer, oBuffer, Ndims, dimSizes, NvalueBytes)
  55. void *iBuffer;
  56. void *oBuffer;
  57. long Ndims;
  58. long dimSizes[];
  59. long NvalueBytes;    /* size (in bytes) of each value */
  60. {
  61. long indices[MAX_DIMS];
  62. long ROWproducts[MAX_DIMS];
  63. long COLproducts[MAX_DIMS];
  64. long Nvalues;
  65. long i, j;
  66.  
  67. /******************************************************************************
  68. * Calculate the number of values in the array.
  69. ******************************************************************************/
  70.  
  71. Nvalues = 1;
  72. for (i = 0; i < Ndims; i++) Nvalues *= dimSizes[i];
  73.  
  74. /******************************************************************************
  75. * Don't do anything except copy if less than 2 dimensions.  Row and column
  76. * major are the same for 0 or 1 dimensions.
  77. ******************************************************************************/
  78.  
  79. if (Ndims < 2) {
  80.   memmove (oBuffer, iBuffer, (size_t) (Nvalues * NvalueBytes));
  81.   return;
  82. }
  83.  
  84. /******************************************************************************
  85. * Calculate the row and column products (what each dimension is worth).
  86. ******************************************************************************/
  87.  
  88. for (i = 0; i < Ndims; i++) {
  89.    ROWproducts[i] = 1;
  90.    for (j = i + 1; j < Ndims; j++) ROWproducts[i] *= dimSizes[j];
  91.    COLproducts[i] = 1;
  92.    for (j = 0; j < i; j++) COLproducts[i] *= dimSizes[j];
  93. }
  94.  
  95. /******************************************************************************
  96. * For each value in the row major array, calculate its indices and then
  97. * calculate the corresponding value offset in the column major array (then
  98. * move it into the column major array).
  99. ******************************************************************************/
  100.  
  101. for (i = 0; i < Nvalues; i++) {
  102.    ROWindices (i, Ndims, ROWproducts, indices);
  103.    COLoffset (indices, Ndims, COLproducts, j);
  104.    memmove ((char *) oBuffer + (j * NvalueBytes),
  105.         (char *) iBuffer + (i * NvalueBytes), (size_t) NvalueBytes);
  106. }
  107.  
  108. return;
  109. }
  110.